#!/usr/bin/perl
#
# Test routine for the places cleaning
#

use Validate::Places;
use Counter;

if ($ARGV[0] =~ /^\d+$/) {
    $recno = shift;
    $file = FileTable::open("db/persons");
    $rec = $file->get($recno) || die "No record $recno\n";
    $clean = {};
    print "----- cleanplace results -----\n";
    Validate::Places::cleanplace($rec, $clean);
    for $field (country, state, city, email, placesource, placeid) {
	print "$field: ", $rec->{$field}, " -> ", $clean->{$field}, "\n";
    }
    print "----- findplace results -----\n";
    $res = Validate::Places::findplace($rec);
    $res2 = Validate::Places::findplace($clean);
    if ($res && !$res2 || $res2 && !$res || $res->key ne $res2->key) {
	print "Dirty and clean gave different results. Cleaning may be good.\n";
    }
    print "Record: ", $res?$res->key . " " . $res->{name}:"none", "\n";
    @res = Validate::Places::findplaces($rec);
    print "Country: ", $res[0]?$res[0]->key:"none", "\n";
    print "State  : ", $res[1]?$res[1]->key:"none", "\n";
    print "City   : ", $res[2]?$res[2]->key:"none", "\n";

    @pretty = Validate::Places::prettynames($clean);
    print "Pretty names:\n";
    
    print "Country: ", $pretty[0], "\n";
    print "State  : ", $pretty[1], "\n";
    print "City   : ", $pretty[2], "\n";
} elsif ($ARGV[0]) {

    $rec = {};
    $rec->{city} = shift;
    $rec->{state} = shift;
    $rec->{country} = shift;
    
    $res = Validate::Places::findplace($rec);
    print "Place found: ";
    print $res->{name} if $res;
    print "\n";
    @res = Validate::Places::findplaces($rec);
    
    print "Found places:\n";
    print "Country: ", $res[0]->key, " ", $res[0]->{longname}, "\n" if $res[0];
    print "State  : ", $res[1]->key, " ", $res[1]->{longname}, "\n" if $res[1];
    print "City   : ", $res[2]->key, " ", $res[2]->{longname}, "\n" if $res[2];
    @pretty = Validate::Places::prettynames($rec);
    print "Pretty names:\n";
    
    print "Country: ", $pretty[0], "\n";
    print "State  : ", $pretty[1], "\n";
    print "City   : ", $pretty[2], "\n";
} else {
    # Test charset translating functionality
    for $ix (0200, 0240, 0300, 0340) {
	$str = "";
	for $ix2 (0..037) {
	    $str .= chr($ix + $ix2);
	}
        print "Lat1  $ix: ", $str, "\n" if $ix >= 0240;
	print "ASCII $ix: ", Validate::Places::asciify($str), "\n";
    }
}







